package com.cppsu.cgx.libraryseatselection.controller;

import com.cppsu.cgx.libraryseatselection.common.api.CommonResult;
import com.cppsu.cgx.libraryseatselection.dto.UserDto;
import com.cppsu.cgx.libraryseatselection.dto.UserLoginParam;
import com.cppsu.cgx.libraryseatselection.dto.UserParam;
import com.cppsu.cgx.libraryseatselection.services.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: guibo
 * @Description:
 * @Date: Created in 11:19 2021/3/10
 * @Modified By:
 */
@Controller
@Api(tags = "用户管理")
@RequestMapping("/user")
@CrossOrigin(origins = "*",maxAge = 3600)
public class UserController {
    @Autowired
    UserService userService;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @ApiOperation("按照条件搜索用户")
    @GetMapping(value = "/list")
    @ResponseBody
    @PreAuthorize("hasAuthority('admin')")
    CommonResult<List<UserDto>> list(UserParam userParam){
        return CommonResult.success(userService.list(userParam));
    }


    @ApiOperation("添加用户")
    @PostMapping(value = "/add")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('admin','user')")
    CommonResult<Integer> add(@RequestBody @Valid UserParam userParam){
        return CommonResult.success(userService.add(userParam));
    }

    @ApiOperation("更新用户")
    @PostMapping(value = "/update")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('admin','user')")
    CommonResult<Integer> update(@RequestBody UserParam userParam){
        return CommonResult.success(userService.update(userParam));
    }

    @ApiOperation("批量删除用户")
    @PostMapping(value = "/delete")
    @ResponseBody
    @PreAuthorize("hasAuthority('admin')")
    CommonResult<Integer> delete(@RequestBody List<Long> ids){
        return CommonResult.success(userService.delete(ids));
    }

    @ApiOperation("获取当前用户")
    @GetMapping(value = "/getCurrentUser")
    @ResponseBody
    CommonResult<UserDto> getCurrentUser(){
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(userService.getCurrentUser(),userDto);
        return CommonResult.success(userDto);
    }


    @ApiOperation(value = "登录以后返回token")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult login(@RequestBody @Valid UserLoginParam userLoginParam) {
        String token = userService.login(userLoginParam);
        if (token == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return CommonResult.success(tokenMap);
    }

    @ApiOperation(value = "更改密码")
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Integer> updatePassword(String oldPassword,String newPassword) {
        return CommonResult.success(userService.updatePassword(oldPassword,newPassword));
    }

}
